package com.hexinfo.dmpro.common.base;

import com.github.pagehelper.PageInfo;
import com.hexinfo.dmpro.common.exception.BadRequestException;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * Created by Administrator on 2019/12/11.
 */
@Slf4j
public class BaseDao extends SqlSessionDaoSupport {

    @Autowired
    public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate){
        super.setSqlSessionTemplate(sqlSessionTemplate);
    }

    public <T> List<T> selectList(Class<T> T, String mappId, Object arg){
        return (List<T>)this.getSqlSessionTemplate().selectList(mappId,arg);
    }
    public <T> List<T> selectList(Class<T> T, String mappId){
        return (List<T>)this.getSqlSessionTemplate().selectList(mappId,null);
    }

    protected <T> List<T> selectPage(Class<T> T, String mappId, Page page, Object obj){
        if(page.getTotal() > 0){
            com.github.pagehelper.page.PageMethod.startPage(page.getPageIndex()+1,page.getPageSize(),false);
        }else{
            com.github.pagehelper.page.PageMethod.startPage(page.getPageIndex()+1,page.getPageSize(),true);
        }

        List<T> retList = this.getSqlSessionTemplate().selectList(mappId,obj);

        if(page.getTotal() <=0){
            PageInfo pageInfo = new PageInfo<>(retList);
            page.setTotal(pageInfo.getTotal());
        }
        return retList;
    }
    protected <T> List<T> selectPage(Class<T> T, String mappId, Page page){
        return selectPage(T,mappId,page,null);
    }
    public <T> T selectOne(Class<T> T,String mappId,Object arg){
        return (T)this.getSqlSessionTemplate().selectOne(mappId,arg);
    }

    protected int insert(String mappId,Object arg){
        return this.getSqlSessionTemplate().insert(mappId,arg) ;
    }

    protected int update(String mappId,Object arg){
        return this.getSqlSessionTemplate().update(mappId,arg) ;
    }
    protected int delete(String mappId,Object arg){
        return this.getSqlSessionTemplate().delete(mappId,arg) ;
    }

    public int executeNamedSql(String nameSqlId,Object arg){
        if(nameSqlId.indexOf("insert") >= 0){
            return this.insert(nameSqlId,arg) ;
        }else  if(nameSqlId.indexOf("update") >= 0){
            return this.update(nameSqlId,arg) ;
        }else  if(nameSqlId.indexOf("delete") >= 0){
            return this.delete(nameSqlId,arg) ;
        }else{
            throw new BadRequestException("未发现合法的nameSqlId=" + nameSqlId) ;
        }
    }

    public int countByNamedSql(String nameSqlId,Object arg){
        Integer num = this.selectOne(Integer.class,nameSqlId,arg) ;
        if(num==null){
            return 0;
        }
        return num;
    }
    public int executeNamedSqlBatch(String nameSqlId,List list){
        int retVal = 0;
        for(Object obj:list){
            retVal+=this.executeNamedSql(nameSqlId,obj) ;
        }
        return retVal;
    }
    public int deleteBatch(String nameSqlId,List list){
        int retVal = 0;
        for(Object obj:list){
            retVal+=this.delete(nameSqlId,obj) ;
        }
        return retVal;
    }
}
